package android.slc.core.ui.fragment.preference;

import android.os.Bundle;
import android.slc.commonlibrary.util.ViewModelProviderFactory;
import android.slc.core.ui.delegate.MvvmViewDelegate;
import android.slc.core.ui.fragment.BaseFragment;
import android.slc.core.vm.BaseViewModel;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

/**
 * @author android.slc
 * @date 2020/3/2 11:04
 */
public abstract class MvvmPreferenceFragment extends BasePreferenceFragment {

    /**
     * 初始化ViewDelegate
     *
     * @param savedInstanceState
     */
    @Override
    protected void onInitViewDelegate(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
        mViewDelegate = new MvvmPreferenceViewDelegate(this);
        mViewDelegate.onCreatePreferences(savedInstanceState, rootKey);
    }

    /**
     * 获取mvvm代理
     *
     * @return
     */
    protected MvvmPreferenceViewDelegate getMvvmViewDelegate() {
        return (MvvmPreferenceViewDelegate) mViewDelegate;
    }

    /**
     * 根据AppProvider创建ViewModel
     * 便于子类调用
     *
     * @param modelClass
     * @param <VM>
     * @return
     */
    protected <VM extends ViewModel> VM getVmByAppProvider(@NonNull Class<VM> modelClass) {
        return getMvvmViewDelegate().getVmByAppProvider(modelClass);
    }

    /**
     * 根据ActivityViewModelProvider创建ViewModel
     * 便于子类调用
     *
     * @param modelClass
     * @param <VM>
     * @return
     */
    protected <VM extends ViewModel> VM getVmByActivityProvider(@NonNull Class<VM> modelClass) {
        return getMvvmViewDelegate().getVmByActivityProvider(modelClass);
    }

    /**
     * 根据FragmentViewModelProvider创建ViewModel
     * 便于子类调用
     *
     * @param modelClass
     * @param <VM>
     * @return
     */
    protected <VM extends ViewModel> VM getVmByFragmentProvider(@NonNull Class<VM> modelClass) {
        return getMvvmViewDelegate().getVmByFragmentProvider(modelClass);
    }

    /**
     * 注册事件
     * 便于子类调用
     *
     * @param viewModel
     */
    protected void registerLiveEvent(BaseViewModel viewModel) {
        getMvvmViewDelegate().registerLiveEvent(viewModel);
    }

}
